package gui;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.*;

import java.sql.SQLException;

import gui.Stu.StuMain;
import info.*;
import sql.Connect;

public class Login {
    LoginForm loginForm = new LoginForm();
    private  User user = new User();
//    //登录
//    private boolean loginQuery(LoginForm loginForm,Connect conn,User user) throws SQLException {
//         user = conn.Login(loginForm);
//
//        if(user !=null){
//            return true;
//        }else{
//            return false;
//        }
//    };

    private User loginQuery(LoginForm loginForm, Connect conn) throws SQLException {
        return conn.Login(loginForm);
    }

    private boolean validateLogin(Connect conn) throws SQLException {
        if (loginForm.getRole()==null){
            JOptionPane.showMessageDialog(null, "角色未选择，请勾选角色", "错误 ", JOptionPane.INFORMATION_MESSAGE);
            return false;
        }
         user = loginQuery(loginForm, conn); // 获取用户对象

        if (user != null){
            System.out.println("登录成功:"+user.getName());
            System.out.println("登录成功");
            return true;
        }else{
            return false;
        }
    }

    public JPanel createLogin(CardLayout cardLayout, JPanel mainPanel,Frame frame, Connect conn){
        //创建头部板块

        JPanel top = new JPanel(new FlowLayout());
        JLabel jl = new JLabel("身份选择");
        JRadioButton jrb1=new JRadioButton("学生");
        JRadioButton jrb2=new JRadioButton("辅导员");
        JRadioButton jrb3=new JRadioButton("任课教师");
        ButtonGroup group=new ButtonGroup();
        group.add(jrb1);
        group.add(jrb2);
        group.add(jrb3);
        top.add(jl);
        top.add(jrb1);
        top.add(jrb2);
        top.add(jrb3);

        //创建登录框和密码框
        JPanel LoginBox = new JPanel(new GridLayout(2,1));
        JPanel NumInput = new JPanel();
        JPanel PwdInput = new JPanel();

        JLabel UserText = new JLabel("用户名");
        JTextField LoginInput = new JTextField("请输入账号",20);
        NumInput.add(UserText);
        NumInput.add(LoginInput);

        JLabel PasswordText = new JLabel("密   码");
        JPasswordField PasswordInput = new JPasswordField("",20);
        PwdInput.add(PasswordText);
        PwdInput.add(PasswordInput);
        LoginBox.add(NumInput);
        LoginBox.add(PwdInput);


        //登录和注册按钮
        JPanel ButtonBox  = new JPanel(new FlowLayout());
        JButton LoginButton = new JButton("登录");
        JButton RegisterButton = new JButton("注册");
        ButtonBox.add(LoginButton);
        ButtonBox.add(RegisterButton);

        //登录框监听，使用回车跳转下一个输入框或按钮
        LoginInput.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode()==KeyEvent.VK_ENTER){
                    PasswordInput.requestFocus();
                }
            }
        });
        //密码框监听，使用回车直接登录
        PasswordInput.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode()==KeyEvent.VK_ENTER){
                    LoginButton.doClick();
                }
            }
        });
        //添加身份认证监听事件
        jrb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginForm.setRole("Stu");
            }
        });
        jrb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginForm.setRole("Counsellor");
            }//private String[] Roles={"Stu","Teacher","Counsellor"};
        });
        jrb3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginForm.setRole("Teacher");
            }
        });
        //添加登录账号密码登录框监听事件
        LoginInput.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                updateNum();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateNum();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateNum();
            }
            private void updateNum() {
                String text = LoginInput.getText();
                if (text == "null" || text.isEmpty()) {
                    loginForm.setNumber(""); // 或设置为默认值
                    return;
                }
                try {
                    loginForm.setNumber(text);
                    System.out.println("输入学号或教职工编号: " + loginForm.getNumber() );
                } catch (NumberFormatException ex) {
                    System.err.println("Invalid number format: " + text);
                    // 处理无效格式的情况
                }
            }
        });
        LoginInput.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (LoginInput.getText().equals("请输入账号")) {
                        LoginInput.setText("");
                }

            }

            @Override
            public void focusLost(FocusEvent e) {

            }
        });
        PasswordInput.getDocument().addDocumentListener(new DocumentListener(){
            @Override
            public void insertUpdate(DocumentEvent e) {
                updatePwd();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updatePwd();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updatePwd();
            }
            private void updatePwd() {
                loginForm.setPwd(PasswordInput.getText());
                System.out.println(" LoginForm.setPwd:"+ loginForm.getPwd());
            }
        });

        // 创建登录页面
        JPanel LoginPage = new JPanel(new GridLayout(3,1));


        //组件添加到面板
        LoginPage.add(top);
        LoginPage.add(LoginBox);
        LoginPage.add(ButtonBox);



        LoginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if(validateLogin(conn)){
                        System.out.println("登录跳转");
                        frame.dispose();
                       new StuMain(conn,user);

                    }else{
                        LoginButton.requestFocusInWindow();
                    }
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        //跳转注册页面
        RegisterButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.show(mainPanel, "Register");
            }
        });
        return LoginPage;
    }

}
